Skip to content

feat: Rule for C extensions#1

Closed
rsartor-cmd wants to merge 12 commits into
mainfrom
py-extension
Closed

feat: Rule for C extensions#1
rsartor-cmd wants to merge 12 commits into
mainfrom
py-extension

Conversation

@rsartor-cmd

@rsartor-cmd rsartor-cmd commented Jun 23, 2026

Copy link
Copy Markdown
Owner

This PR introduces a new rule, py_extension, used for building C extensions. For now, the macro and rule are sufficiently developed for py_extension_test to build and pass, though work remains to be done for them to be production-ready.

bazel-contrib#3283

@rsartor-cmd rsartor-cmd changed the title Py extension feat: Rule for C extensions Jun 23, 2026
…el-contrib#3851)

This builds on the existing `py-extension` branch. It adds support for
platform and abi tags in the resulting library filename. The
`:py_extension_test`, `:py_extension_analysis_tests`, and
`:py_limited_api_tests` test targets in `//tests/cc/py_extension` now
build and pass.

Relates to bazel-contrib#3283
@rsartor-cmd
rsartor-cmd force-pushed the py-extension branch 2 times, most recently from acf55b4 to aab3cee Compare July 15, 2026 17:22
…tension (bazel-contrib#3875)

Per feedback from bazel-contrib#3851, this PR re-works the platform detection logic
to rely on platform constraints instead of the Python runtime/toolchain.

### Description

This PR contains updates to the experimental `py_extension`
implementation. It re-works the platform tag detection to rely on modern
platform constraints, and refactors the compilation and linking
mechanism
to delegate to Bazel's native `cc_shared_library` and `cc_library`
rules.

This PR targets the main repository's `py-extension` branch (not
`main`).

### Key Changes

#### 1. Platform Tag & ABI Tag Derivation
* **Introduced `abi_tag` to `PyCcToolchainInfo`**: Added the `abi_tag`
field to the `PyCcToolchainInfo` provider, populated by the
`py_cc_toolchain` rule. It defaults to deriving the tag from
`python_version` (e.g. `cpython-311`) for backward compatibility.
* **Constraint-Based Detection**: Replaced legacy parsing of
`cc_toolchain` CPU names with direct lookup in rules_python's central
`PLATFORMS` registry using platform constraints.
* **Limited API Configuration**: Updated how limited API (`.abi3.so`)
suffixes are appended based on the toolchain configuration.

#### 2. Compilation & Linking Delegation (Refactor to
`cc_shared_library`)
* **Macro Fusion**: Replaced the custom linking logic in the
`py_extension` rule with a macro of the same name. It now accepts C/C++
source/header files directly (`srcs`, `hdrs`, `copts`, `defines`),
implicitly wrapping them in a private `cc_library` under the hood.
* **Consolidated Attributes**: Consolidated static linkage under
standard `deps` (removing the redundant `static_deps` attribute) and
aligned linker arguments with `cc_shared_library`'s `user_link_flags`.
* **Wrapper Rule (`_py_extension_wrapper`)**: Added a lightweight,
private rule that wraps the `cc_shared_library` output to:
* Query the Python toolchain for platform/ABI tags to resolve PEP 3149
compliant filenames (e.g. `.cpython-311-x86_64-linux-gnu.so` or
`.abi3.so`).
*   Create a cheap `symlink` from the CSL output to the PEP 3149 name.
* Propagate `PyInfo` (for python rules) and `CcSharedLibraryInfo` (for
dynamic C++ dependencies).
* **ODR Validation**: Leveraging native `cc_shared_library` ensures
strict analysis-time validation against One Definition Rule (ODR)
violations (e.g., duplicate static linkage in dynamic chains).

#### 3. Runfiles and Data Support
* **Runfiles Propagation**: Re-implemented runfiles collection using the
repository's standard `builders.RunfilesBuilder` to support runtime
assets (`data` attribute) and dynamic library dependency
propagation.

### Verification & Testing
* Added parity test cases in `dependency_graph_tests.bzl` to verify
dynamic dependency chains and static sharing behavior.
* Added runfiles validation in `py_extension_tests.bzl` to verify data
asset propagation.
*   Updated limited API tests to match CSL output structure.
* All 14 tests in `//tests/cc/py_extension/...` are compiling and
passing.
──────
TAG=agy
CONV=23f8a5e8-2d99-401a-9903-1256b7d42b0e

---------

Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
@rsartor-cmd
rsartor-cmd force-pushed the py-extension branch 2 times, most recently from 549bc49 to ae12049 Compare July 21, 2026 02:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants